namespace Peer.Web.Controls.Upload
{
    using System;
    using System.IO;

    public class UploadFile
    {
        private string m_clientName;
        private string m_contenttype;
        private long m_filelength;
        private string m_filename;
        private string m_filePath;

        public UploadFile(string i_name)
        {
            if ((i_name != null) && (i_name != string.Empty))
            {
                string str = string.Empty;
                this.m_clientName = string.Empty;
                this.m_filename = string.Empty;
                this.m_contenttype = string.Empty;
                this.m_filelength = 0L;
                if (this.IsContentHeader(i_name))
                {
                    str = i_name;
                }
                else if (this.IsContentHeader(WebbHelper.GetContext().Request[i_name]))
                {
                    str = WebbHelper.GetContext().Request[i_name];
                }
                if ((str != null) && (str != string.Empty))
                {
                    string[] strArray = str.Split(new char[] { ';' });
                    string str2 = strArray[0];
                    this.m_contenttype = str2.Substring(str2.IndexOf(":") + 1).Trim();
                    str2 = strArray[1];
                    this.m_clientName = str2.Substring(str2.IndexOf("\"") + 1, (str2.LastIndexOf("\"") - str2.IndexOf("\"")) - 1).Trim();
                    str2 = strArray[2];
                    this.m_filename = str2.Substring(str2.IndexOf("\"") + 1, (str2.LastIndexOf("\"") - str2.IndexOf("\"")) - 1).Trim();
                    string uploadFolder = WebbHelper.GetUploadFolder();
                    if ((this.m_filename != null) && (this.m_filename != string.Empty))
                    {
                        this.m_filePath = Path.Combine(uploadFolder, this.m_filename);
                        try
                        {
                            this.m_filelength = new FileInfo(this.m_filePath).Length;
                        }
                        catch (Exception exception)
                        {
                            string str4 = WebbHelper.GetContext().Request["Webb_Upload_GUID"];
                            if (str4 != null)
                            {
                                WebbHelper.GetContext().Application.Remove("Upload_Status_" + str4);
                            }
                            throw exception;
                        }
                    }
                }
            }
        }

        private bool IsContentHeader(string i_line)
        {
            if ((i_line == null) || (i_line == string.Empty))
            {
                return false;
            }
            string[] strArray = i_line.Split(new char[] { ';' });
            return (((strArray.Length == 3) && (strArray[0].IndexOf("Content-Type:") >= 0)) && ((strArray[1].IndexOf("filename=\"") >= 0) && (strArray[2].IndexOf("filePath=\"") >= 0)));
        }

        public void SaveAs(string filename)
        {
            string str = WebbHelper.GetContext().Request["Webb_Upload_GUID"];
            if(string.IsNullOrEmpty(Path.GetFileName(filename)))
                return;
            try
            {
                UploadStatus status;
                FileInfo info = new FileInfo(this.m_filePath);
                if (str != null)
                {
                    status = new UploadStatus();
                    status.GetUploadStatus(str);
                    status.Status = UploadStatus.UploadState.Moving;
                    WebbHelper.GetContext().Application["Upload_Status_" + str] = status;
                }
                string directoryName = Path.GetDirectoryName(filename);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }
                else if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                info.MoveTo(filename);
                if (str != null)
                {
                    status = new UploadStatus();
                    status.GetUploadStatus(str);
                    status.Status = UploadStatus.UploadState.Completed;
                    WebbHelper.GetContext().Application["Upload_Status_" + str] = status;
                }
            }
            catch (Exception exception)
            {
                if (str != null)
                {
                    WebbHelper.GetContext().Application.Remove("Upload_Status_" + str);
                }
                throw exception;
            }
        }

        public string ClientFullPathName
        {
            get
            {
                return this.m_clientName;
            }
        }

        public string ContentType
        {
            get
            {
                return this.m_contenttype;
            }
        }

        public string ExtendName
        {
            get
            {
                return this.m_filePath.Substring(this.m_filename.LastIndexOf("."));
            }
        }

        public string FileName
        {
            get
            {
                return this.m_filename;
            }
        }

        public long FileSize
        {
            get
            {
                return this.m_filelength;
            }
        }
    }
}

